var initSummernote = function(){
    $('#summernote').summernote({
        toolbar: [],
        callbacks: {
            onInit: function(e) {
                $("#summernote").summernote("fullscreen.toggle");
                $('#summernote').summernote('fontName', 'Helvetica Neue');
            }
        }
    });
}

var focus = function(){
    $('#summernote').summernote('focus');
}

var disable = function(){
    $('#summernote').summernote('disable');
}

var enable = function(){
    $('#summernote').summernote('enable');
}

var pasteHTML = function(html){
    $('#summernote').summernote('code',html);
    keepLastIndex(document.getElementsByClassName('note-editable panel-body')[0]);
}

function keepLastIndex(obj) {
    var range = window.getSelection();//创建range
    range.selectAllChildren(obj);//range 选择obj下所有子内容
    range.collapseToEnd();//光标移至最后
}

var refreshHTML = function(){
    MRichEditor.returnHtml($('#summernote').summernote('code'));
}

var undo = function() {
    $('#summernote').summernote('undo');
};

var redo = function() {
    $('#summernote').summernote('redo');
};

var disable = function() {
    $('#summernote').summernote('disable');
};

var enable = function() {
    $('#summernote').summernote('enable');
};

var bold = function() {
    $('#summernote').summernote('bold');
};

var italic = function() {
    $('#summernote').summernote('italic');
};

var underline = function() {
    $('#summernote').summernote('underline');
};

var strikethrough = function() {
    $('#summernote').summernote('strikethrough');
};

var superscript = function() {
    $('#summernote').summernote('superscript');
};

var subscript = function() {
    $('#summernote').summernote('subscript');
};

var backColor = function(color) {
    $('#summernote').summernote('backColor', color);
};

var foreColor = function(color) {
    $('#summernote').summernote('foreColor', color);
};

var fontName = function(fontName) {
    $('#summernote').summernote('fontName', fontName);
};

var fontSize = function(fontSize) {
    $('#summernote').summernote('fontSize', fontSize);
};

var justifyLeft = function() {
    $('#summernote').summernote('justifyLeft');
};

var justifyRight = function() {
    $('#summernote').summernote('justifyRight');
};

var justifyCenter = function() {
    $('#summernote').summernote('justifyCenter');
};

var justifyFull = function() {
    $('#summernote').summernote('justifyFull');
};

var insertOrderedList = function() {
    $('#summernote').summernote('insertOrderedList');
};

var insertUnorderedList = function() {
    $('#summernote').summernote('insertUnorderedList');
};

var indent = function() {
    $('#summernote').summernote('indent');
};

var outdent = function() {
    $('#summernote').summernote('outdent');
};

var formatBlock = function(tagName) {
    $('#summernote').summernote('formatBlock', tagName);
};

var formatPara = function() {
    $('#summernote').summernote('formatPara');
};

var formatH1 = function() {
    $('#summernote').summernote('formatH1');
};

var formatH2 = function() {
    $('#summernote').summernote('formatH2');
};

var formatH3 = function() {
    $('#summernote').summernote('formatH3');
};

var formatH4 = function() {
    $('#summernote').summernote('formatH4');
};

var formatH5 = function() {
    $('#summernote').summernote('formatH5');
};

var formatH6 = function() {
    $('#summernote').summernote('formatH6');
};

var lineHeight = function(lineHeight) {
    $('#summernote').summernote('lineHeight', lineHeight);
};

var insertImageUrl = function(imageUrl) {
    $('#summernote').summernote('insertImage', imageUrl, null);
};

var insertText = function(text) {
    $('#summernote').summernote('insertText', text);
};

var createLink = function(linkText, linkUrl) {
    $('#summernote').summernote('createLink', {
      text: linkText,
      url: linkUrl,
      isNewWindow: false
    });
};

var unlink = function() {
    $('#summernote').summernote('unlink');
};

var insertText = function(text) {
    $('#summernote').summernote('editor.insertText', text);
};

var codeView = function(){
    $('#summernote').summernote('codeview.toggle');
}

var insertTable = function(dim){
    $('#summernote').summernote('insertTable', dim);
}

var insertHorizontalRule = function() {
    $('#summernote').summernote('insertHorizontalRule');
}




var getDocx = function() {
    var html = $('#summernote').summernote('code');
    var downloadData = htmlDocx.asBlob(html, {orientation: orientation});
     var reader = new window.FileReader();
     reader.onloadend = function() {
             var base64data = reader.result;
             MRichEditor.returnDocx(base64data)
     }
     reader.readAsDataURL(downloadData);
}


var getHtmlAndDocx = function() {
    let json =  window.luckysheet.toJson();
    LuckyExcel.transformLucyToExcelBase64(json,function (filebase64) {
        console.log("filebase64:");
        console.log(filebase64);
        MRichEditor.returnHtmlAndDocx(JSON.stringify(json),filebase64);
    });
}

var  initLuckysheet =function(options){
    var fullconfig1 = {
        undoRedo: false, //撤销重做，注意撤消重做是两个按钮，由这一个配置决定显示还是隐藏
        paintFormat: false, //格式刷
        currencyFormat: false, //货币格式
        percentageFormat: false, //百分比格式
        numberDecrease: false, // '减少小数位数'
        numberIncrease: false, // '增加小数位数
        moreFormats: false, // '更多格式'
        font: false, // '字体'
        fontSize: false, // '字号大小'
        bold: false, // '粗体 (Ctrl+B)'
        italic: false, // '斜体 (Ctrl+I)'
		strikethrough: false, // '删除线 (Alt+Shift+5)'
		underline: false, // '下划线 (Alt+Shift+6)'
        textColor: false, // '文本颜色'
        fillColor: false, // '单元格颜色'
        border: false, // '边框'
        mergeCell: false, // '合并单元格'
        horizontalAlignMode: false, // '水平对齐方式'
        verticalAlignMode: false, // '垂直对齐方式'
        textWrapMode: false, // '换行方式'
        textRotateMode: false, // '文本旋转方式'
		image:false, // '插入图片'
		link:false, // '插入链接'
        chart: false, // '图表'（图标隐藏，但是如果配置了chart插件，右击仍然可以新建图表）
        postil:  false, //'批注'
        pivotTable: false,  //'数据透视表'
        function: false, // '公式'
        frozenMode: false, // '冻结方式'
        sortAndFilter: false, // '排序和筛选'
        conditionalFormat: false, // '条件格式'
		dataVerification: false, // '数据验证'
        splitColumn: false, // '分列'
        screenshot: false, // '截图'
        findAndReplace: false, // '查找替换'
		protection:false, // '工作表保护'
		print:false, // '打印'
    }
    var fullconfig2 =[   
		"undo", "redo", "paintFormat", "|", 
		"currencyFormat", "percentageFormat", "numberDecrease", "numberIncrease", "moreFormats", "|", 
		"font", "|", 
		"fontSize", "|", 
		"bold", "italic", "strikethrough", "underline", "textColor", "|", 
		"fillColor", "border", "mergeCell", "|", 
		"horizontalAlignMode", "verticalAlignMode", "textWrapMode", "textRotateMode", "|", 
		"image", "link", "chart", "postil", "pivotTable", "|", 
		"function", "frozenMode", "sortAndFilter", "conditionalFormat", "dataVerification", "splitColumn", "screenshot", "findAndReplace", "protection", "print"
	];
    var tbconfig = 	[   
		"undo", "redo", "|", 
		"currencyFormat", "percentageFormat",  "moreFormats", "|", 
		"font", "|", 
		"fontSize", "|", 
		"bold", "italic", "strikethrough", "underline", "textColor", "|", 
		"fillColor", "border", "mergeCell", "|", 
		"horizontalAlignMode", "verticalAlignMode", "textWrapMode", "textRotateMode", "|", 
		"image", "link",  "postil",  "|", 
		"function", "frozenMode", "sortAndFilter",  "findAndReplace"
	]
    var defaultOpt = {
        container: "luckysheet",
        showtoolbar: true,
        showtoolbarConfig: tbconfig,
        showinfobar: false,
        lang: 'zh',
    };
    
    if(typeof options == "object" && Object.prototype.toString.call(options)=="[object Object]"){
        luckysheet.create(options);
    }else{
        luckysheet.create(defaultOpt);
    }
}


var initLuckysheetFromFileBase64 = function(filebase64,name){
    LuckyExcel.transformExcelFileBase64ToLucky(filebase64, name, function (exportJson, luckysheetfile) {
        if (exportJson.sheets == null || exportJson.sheets.length == 0) {
            alert("Failed to read the content of the excel file, currently does not support xls files!");
            return;
        }
        console.log(exportJson, luckysheetfile);
        var tbconfig = 	[   
            "undo", "redo", "|", 
            "currencyFormat", "percentageFormat",  "moreFormats", "|", 
            "font", "|", 
            "fontSize", "|", 
            "bold", "italic", "strikethrough", "underline", "textColor", "|", 
            "fillColor", "border", "mergeCell", "|", 
            "horizontalAlignMode", "verticalAlignMode", "textWrapMode", "textRotateMode", "|", 
            "image", "link",  "postil",  "|", 
            "function", "frozenMode", "sortAndFilter",  "findAndReplace"
        ]
        window.luckysheet.destroy();
        window.luckysheet.create({
            container: 'luckysheet', //luckysheet is the container id
            showtoolbar: true,
            showtoolbarConfig: tbconfig,
            showinfobar: false,
            lang: 'zh',
            data: exportJson.sheets,
            title: exportJson.info.name,
            userInfo: exportJson.info.name.creator
        });
    });
}